package com.hsav.kcxb.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;

import com.hsav.kcxb.R;
import com.hsav.kcxb.adapter.SongListAdapter;
import com.hsav.kcxb.main.MSYS;
import com.kcbsdk.EventHandler;
import com.kcbsdk.MyRankBa.AObbligatoList;
import com.kcbsdk.MyTools.ACloudType;
import com.kcbsdk.MyTools.ALocalInfoList;
import com.kcbsdk.MyTools.AUiPageName;

import static com.hsav.kcxb.main.MSYS.MSTRING;
import static com.hsav.kcxb.main.MSYS.mMyRankBa;
import static com.hsav.kcxb.main.MSYS.mMyTools;

public class RecordActivity extends BaseActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    private ImageView imgSolo;
    private TextView textSolo;
    private ImageView imgChorus;
    private TextView textChorus;
    private ImageView imgKGe;
    private TextView textKGe;
    private View layoutTipsSolo;
    private View layoutTipsChorus;
    private View layoutKGe;
    private ImageView imgModeMusic;
    private ImageView imgModeVideo;
    private ALocalInfoList mALocalInfoList;
    private boolean isLoading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aty_record);
        initView();
    }

    private void initView() {
        LinearLayout itemSolo = (LinearLayout) findViewById(R.id.item_solo);
        itemSolo.setOnClickListener(this);
        imgSolo = (ImageView)findViewById(R.id.img_solo);
        imgSolo.setImageDrawable(getResources().getDrawable(R.drawable.sing_single_highlight));
        textSolo = (TextView)findViewById(R.id.text_solo);
        textSolo.setTextColor(getResources().getColor(R.color.colorAccent));
        LinearLayout itemChorus = (LinearLayout) findViewById(R.id.item_chorus);
        itemChorus.setOnClickListener(this);
        imgChorus = (ImageView)findViewById(R.id.img_chorus);
        textChorus = (TextView)findViewById(R.id.text_chorus);
        LinearLayout itemKGe = (LinearLayout) findViewById(R.id.item_kGe);
        itemKGe.setOnClickListener(this);
        imgKGe = (ImageView)findViewById(R.id.img_kGe);
        textKGe = (TextView)findViewById(R.id.text_kGe);
        LinearLayout itemMix = (LinearLayout) findViewById(R.id.layout_mix);
        itemMix.setOnClickListener(this);

        layoutTipsSolo = findViewById(R.id.layout_tips_solo);
        layoutTipsChorus = findViewById(R.id.layout_tips_chorus);
        layoutTipsChorus.setVisibility(View.GONE);
        layoutKGe = findViewById(R.id.layout_kGe);
        layoutKGe.setVisibility(View.GONE);

        imgModeMusic = (ImageView)findViewById(R.id.img_mode_music);
        imgModeMusic.setImageDrawable(getResources().getDrawable(R.drawable.record_music_highlight));

        imgModeVideo = (ImageView)findViewById(R.id.img_mode_video);
        imgModeVideo.setImageDrawable(getResources().getDrawable(R.drawable.record_machine_normal));

        Switch switchMode = (Switch) findViewById(R.id.switch_mode);
        switchMode.setOnCheckedChangeListener(this);
        switchMode.setChecked(false);

        TextView btnStart = (TextView) findViewById(R.id.btn_start);
        btnStart.setOnClickListener(this);

        ListView listSongs = (ListView) findViewById(R.id.list_songs);
        listSongs.setAdapter(new SongListAdapter(this));

        initKcbData();
    }
    private void initKcbData(){
        mMyTools.setUiEventCallBack(this, new EventHandler(){
            @Override
            public void onComplete(int methodType, boolean isFail, String message){
                if (methodType > 0){
                    if (isFail){
                        mMyTools.Toast(String.format("%s%s(%08x)", getString(R.string.methodError), message, methodType));
                    }
                    else {
                    }
                } else{
                    if (mMyTools.isEventLocalInfoDbs(ACloudType.TYPE_KBA, AUiPageName.PAGE_READY_SING)){
                        mALocalInfoList = mMyTools.mAReadySingList[0];
                        if (mALocalInfoList  != null){
                            if (!isLoading){
                                mMyTools.Log(String.format("PAGE_READY_SING %s %s",mALocalInfoList.SongName, mALocalInfoList.IsComplete));
                                if (!mALocalInfoList.IsComplete) {           // 之前还没有完成
                                    mMyTools.Log(String.format("fetchObbligaDetail %d %d",mALocalInfoList.MusicID0, mALocalInfoList.MusicID1));
                                    mMyRankBa.fetchObbligaDetail(mALocalInfoList.MusicID0, mALocalInfoList.MusicID1);
                                }
                            }
                            else {
                                mMyTools.Log(String.format("下载中 %s %d %d",mALocalInfoList.SongName, mALocalInfoList.FileCounter, mALocalInfoList.FileLength));
                            }
                        }
                    }
                }
            }
        });
        mMyRankBa.setUiEventCallBack(this, new EventHandler(){
            @Override
            public void onComplete(int methodType, boolean isFail, String message){
                if (methodType > 0){
                    if (isFail){
                        mMyRankBa.Toast(String.format("%s%s(%08x)", getString(R.string.methodError), message, methodType));
                    }
                    else {
                        if (mMyRankBa.isSucessFetchObbligaDetail(methodType)){
                            MSTRING(String.format("isSucessFetchObbligaDetail %s", mMyRankBa.mAObbligDetail.SongName));
                            mMyTools.loadingLocalInfoItem(AUiPageName.PAGE_READY_SING, 0);
                            isLoading = true;
                        }
                    }
                } else{
                }
            }
        });
        isLoading = false;
        mMyTools.ReadyToSing(ACloudType.TYPE_KBA, MSYS.getAObbligatoList());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.item_solo:
                imgSolo.setImageDrawable(getResources().getDrawable(R.drawable.sing_single_highlight));
                textSolo.setTextColor(getResources().getColor(R.color.colorAccent));
                imgChorus.setImageDrawable(getResources().getDrawable(R.drawable.sing_he_normal));
                textChorus.setTextColor(getResources().getColor(R.color.color_white_normal));
                imgKGe.setImageDrawable(getResources().getDrawable(R.drawable.sing_ktv_normal));
                textKGe.setTextColor(getResources().getColor(R.color.color_white_normal));

                layoutTipsSolo.setVisibility(View.VISIBLE);
                layoutTipsChorus.setVisibility(View.GONE);
                layoutKGe.setVisibility(View.GONE);
                break;
            case R.id.item_chorus:
                imgChorus.setImageDrawable(getResources().getDrawable(R.drawable.sing_he_highlight));
                textChorus.setTextColor(getResources().getColor(R.color.colorAccent));
                imgSolo.setImageDrawable(getResources().getDrawable(R.drawable.sing_single_normal));
                textSolo.setTextColor(getResources().getColor(R.color.color_white_normal));
                imgKGe.setImageDrawable(getResources().getDrawable(R.drawable.sing_ktv_normal));
                textKGe.setTextColor(getResources().getColor(R.color.color_white_normal));

                layoutTipsChorus.setVisibility(View.VISIBLE);
                layoutTipsSolo.setVisibility(View.GONE);
                layoutKGe.setVisibility(View.GONE);
                break;
            case R.id.item_kGe:
                imgKGe.setImageDrawable(getResources().getDrawable(R.drawable.sing_ktv_highlight));
                textKGe.setTextColor(getResources().getColor(R.color.colorAccent));
                imgSolo.setImageDrawable(getResources().getDrawable(R.drawable.sing_single_normal));
                textSolo.setTextColor(getResources().getColor(R.color.color_white_normal));
                imgChorus.setImageDrawable(getResources().getDrawable(R.drawable.sing_he_normal));
                textChorus.setTextColor(getResources().getColor(R.color.color_white_normal));

                layoutKGe.setVisibility(View.VISIBLE);
                layoutTipsSolo.setVisibility(View.GONE);
                layoutTipsChorus.setVisibility(View.GONE);
                break;
            case R.id.layout_mix:
                startActivity(new Intent(this, MixActivity.class));
                break;
            case R.id.btn_start:
                startActivity(new Intent(this, RunningRecordActivity.class));
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked)
        {
            imgModeVideo.setImageDrawable(getResources().getDrawable(R.drawable.record_machine_highlight));
            imgModeMusic.setImageDrawable(getResources().getDrawable(R.drawable.record_music_normal));
        }
        else
        {
            imgModeVideo.setImageDrawable(getResources().getDrawable(R.drawable.record_machine_normal));
            imgModeMusic.setImageDrawable(getResources().getDrawable(R.drawable.record_music_highlight));
        }
    }
}
